package com.waitingsnow.modules.shell.view.playGift
{
import com.waitingsnow.core.display.player.BmpDataPlayer;
import com.waitingsnow.core.display.loader.BaseLoader;
import com.waitingsnow.core.model.config.AppConfig;
import com.waitingsnow.core.model.vo.GiftVO;

import flash.display.Sprite;
import flash.events.Event;

/**
 * 
 * @author waitingsnow
 * $Id: PlayGiftView.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class PlayGiftView extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>PlayGiftView</code> instance.
	 * 
	 */
	public function PlayGiftView()
	{
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var giftMcObj:Object = {};
	private var baseLoader:BaseLoader;
	private var bmpDataPlayArr:Array = [];
	private var itemWidth:int = 90;
	private var giftWidth:int = 50;
	private var playCount:int = 100;
	private var runCount:int;
	private var totalCount:int;
	private var curBmpDataArr:Array;
	
	//==========================================================================
	//  Properties
	//==========================================================================
	public function get isRun():Boolean
	{
		return (totalCount > runCount);
	}
	
	//==========================================================================
	//  Methods
	//==========================================================================
	public function playGift(giftVO:GiftVO, count:int):void
	{
		var bmpDataArr:Array = giftMcObj[giftVO.giftID];
		totalCount += count;
		if (bmpDataArr)
		{
			startPlay(bmpDataArr, count);
		}
		else
		{
			baseLoader = new BaseLoader();
			baseLoader.paramObject = {vo:giftVO, count:count};
			baseLoader.addEventListener(Event.COMPLETE, baseLoaderCompleteHandler);
			baseLoader.startLoad(giftVO.swfPath, null, true);
		}
	}
	
	private function startPlay(bmpDataArr:Array, count:int):void
	{
		count = Math.min(count, playCount - runCount);
		curBmpDataArr = bmpDataArr;
		for (var i:int = 0; i < count; i++)
		{
			runCount++;
			startPlayGift(bmpDataArr);
		}
	}
	
	private function startPlayGift(bmpDataArr:Array):void
	{
		var bmpDataPlayer:BmpDataPlayer = getBmpDataPlayer(bmpDataArr);
		var w:int = (stage && stage.stageWidth) ? stage.stageWidth : AppConfig.swfWidth;
		var h:int = (stage && stage.stageHeight) ? stage.stageHeight : AppConfig.swfHeight;
		h = h - AppConfig.bottomHeight;
		var randomX:Number;
		var randomY:Number;
		if (Math.random() < 0.5)
		{
			randomX = Math.random() * (w - itemWidth);
			randomY = h - Math.random() * giftWidth - itemWidth;
		}
		else
		{
			randomX = Math.random()*giftWidth*2;
			randomY = Math.random()*(h - itemWidth);
			randomX = (randomX > giftWidth) ? (w-randomX+giftWidth-itemWidth) : randomX;
		}
		bmpDataPlayer.x = randomX;
		bmpDataPlayer.y = randomY;
		bmpDataPlayer.addEventListener(Event.COMPLETE, bmpDataPlayerHandler);
		bmpDataPlayer.play();
		addChild(bmpDataPlayer);
	}
	
	private function getBmpDataPlayer(bmpDataArr:Array):BmpDataPlayer
	{
		var result:BmpDataPlayer;
		if (bmpDataPlayArr && bmpDataPlayArr.length)
		{
			result = bmpDataPlayArr.pop();
		}
		else
		{
			result = new BmpDataPlayer();
		}
		result.bmpDataArr = bmpDataArr;
		return result;
	}
	
	private function collectBmpDataPlayer(bmpDataPlayer:BmpDataPlayer):void
	{
		if (bmpDataPlayer && bmpDataPlayer.parent)
		{
			bmpDataPlayer.parent.removeChild(bmpDataPlayer);
		}
		if (bmpDataPlayArr)
		{
			bmpDataPlayArr.push(bmpDataPlayer);
		}
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function baseLoaderCompleteHandler(event:Event):void
	{
		var bmpDataArr:Array = event.target.getBmpDataArr();
		giftMcObj[baseLoader.paramObject.vo.giftID] = bmpDataArr;
		startPlay(bmpDataArr, baseLoader.paramObject.count);
	}
	
	private function bmpDataPlayerHandler(event:Event):void
	{
		totalCount --;
		runCount --;
		collectBmpDataPlayer(BmpDataPlayer(event.target));
		if (isRun)
		{
			startPlay(curBmpDataArr, 1);
		}
		else
		{
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}
}